package com.easyadmin.pro.modules.sys.controller;

import com.easyadmin.pro.common.constants.HttpConstants;
import com.easyadmin.pro.common.controller.BaseController;
import com.easyadmin.pro.common.vo.RestResult;
import com.easyadmin.pro.modules.sys.entity.DataDictionaryEntity;
import com.easyadmin.pro.tool.DataDictionaryUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * packageName com.easyadmin.pro.modules.sys.controller
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className RedisCacheController
 * @date 2024/8/26
 * @description TODO
 */
@RestController
@RequestMapping(HttpConstants.REDIS_MODEL_PATH)
public class RedisCacheController extends BaseController {

    /**
     * 获取全部缓存数据
     * @return
     */
    @GetMapping("cache")
    public RestResult cache() {
        Map<String, List<DataDictionaryEntity>> dicAllMap = DataDictionaryUtils.getDataDicAllMap();
        Map<String, Map<String, String>> resultMap = new HashMap<>(dicAllMap.size());
        dicAllMap.forEach((k, v) -> {
            Map<String, String> valueMap = v.stream().collect(Collectors.toMap(DataDictionaryEntity::getDataValue
                    , DataDictionaryEntity::getDataValueName));
            resultMap.put(k, valueMap);
        });
        return success(resultMap);
    }

    /**
     * 根据编码及内容获取中文
     * @param code
     * @param value
     * @return
     */
    @GetMapping("codeConventName")
    public RestResult codeConventName(@RequestParam String code,
                                      @RequestParam String value) {
        String dataDicName = null;
        Map<String, List<DataDictionaryEntity>> dicAllMap = DataDictionaryUtils.getDataDicAllMap();
        List<DataDictionaryEntity> dictionaryInfos = dicAllMap.get(code);
        if (!CollectionUtils.isEmpty(dictionaryInfos)) {
            Map<String, String> dataDicInfoMap = dictionaryInfos.stream().collect(Collectors
                    .toMap(DataDictionaryEntity::getDataValue, DataDictionaryEntity::getDataValueName));
            dataDicName = dataDicInfoMap.get(value);
        }
        return success(dataDicName);
    }
}